
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author LONGBK
 */

public class BarMap {
    private int width, height;
    private Image barIMG;
    private TiledLayer map;
    private TiledLayer quay;
    private TiledLayer buc;
    private Image bieutuongIMG;
    private Image banIMG;
    private Image gheIMG;
    private Sprite banghecay;
    private Sprite hieuung;
    private Image cayIMG;
    private int[][] tiles;
    private int[][] quayTiles;
    private int mrLX, mrLY; // toa do cua MrL
    private Vector listbanghecay;
    private int[][] mapXY;
    private int viewPortY;
    private int viewPortX;
    private int savePortY;
    private int savePortX;
    private Image npcIMG;
    private NPC npc;
    public boolean iswar;
    public boolean talking;
    public boolean useBom;
    private boolean bomno;
    private int timeForBomno;
    private Image noIMG;
    private Sprite no;
    private Image bomIMG;
    private Sprite bom;
    private int fv;
    private boolean vibrate;
    private Image chiduongIMG;
    private Sprite chiduong;
    private Image maubanIMG;
    private Sprite mauban;
    public int tgcount;
    public boolean isDrawTime;
    public boolean isDrawing;
    private byte[] textT;
    public int endTextID;
    public String[] talk;
    public int talkIndex;
    private boolean talkBoss;
    private boolean bossmove;
    private boolean demove;
    private boolean blood;
    public BarMap(){

    }
    public void loadmap(SunnetGameCanvas gameCanvas){
        width = gameCanvas.getWidth();
        height = gameCanvas.getHeight();
        listbanghecay = new Vector(27);
        mapXY = new int[][]{{12, 7}, {12, 8}, {13, 10}, {15, 10}, {6, 23}, {6, 25}, {6, 27}, {8, 23}, {8, 25},  {8, 27}, 
                            {10, 23}, {10, 25}, {10, 27}, {12, 23}, {12, 25}, {12 , 27}, {6, 24}, {6, 28}, {10, 24}, {10, 28},
                            {6, 5}, {6, 11}, {11, 6}, {22, 5}, {18, 18}, {13, 17}, {22, 28}
                            };
        talk = new String[]{"Bom!!!Sao các ngươi dám?", "Câu này ta phải hỏi chúng mày đấy?","Đây là địa bàn của bọn ta? Chúng mày mau cút đi!!", "Đồ láo xược!Hãy xem đây!", "Chỗ này rất nguy hiểm!Xin ông chủ rời nhanh khỏi đây!"};
        tiles = new int[][]{
                { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
                { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
                { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
                { 6, 6, 6, 6, 6, 6, 60, 6, 6, 6, 60, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 54, 45, 6, 6 },
                { 6, 6, 6, 6, 6, 6, 59, 6, 6, 6, 59, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 55, 50, 6, 6 },
                { 6, 6, 6, 56, 6, 6, 58, 6, 6, 6, 58, 6, 6, 6, 6, 56, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
                { 9, 9, 9, 57, 9, 10, 4, 4, 4, 4, 4, 8, 9, 9, 9, 57, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 84, 84, 84, 84, 84, 20, 6, 1, 2, 3, 6, 19, 84, 84, 84, 84, 84, 15, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 24, 25, 0, 0, 24, 25, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 29, 30, 0, 0, 29, 30, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 34, 35, 0, 0, 34, 35, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 47, 48, 40, 40, 40, 39, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 80, 80, 80, 80, 80, 80, 80, 80, 80, 5, 80, 52, 53, 44, 40, 40, 39, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
                { 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 49, 40, 40, 39, 19, 84, 15, 9, 13, 14, 14, 14, 14, 14, 14, 14 },
                { 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 27, 27, 28, 26, 83, 76, 75, 78, 83, 83, 83, 83, 83, 83, 83 },
                { 80, 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 26, 83, 76, 75, 78, 83, 11, 11, 83, 60, 83, 60 },
                { 80, 80, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80, 26, 83, 76, 75, 78, 83, 16, 16, 83, 58, 83, 59 },
                { 80, 80, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80, 43, 42, 76, 75, 78, 83, 21, 21, 83, 83, 83, 58 },
                { 40, 41, 40, 31, 32, 33, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 5, 80, 80, 76, 77, 78, 80, 80, 80, 80, 80, 80, 5 },
                { 40, 46, 40, 36, 37, 38, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 74, 75, 79, 80, 80, 80, 80, 80, 80, 80 },
                { 40, 51, 40, 40, 40, 81, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 80 },
                { 40, 40, 40, 40, 39, 82, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80 },
                { 27, 27, 27, 27, 28, 80, 80, 80, 80, 80, 5, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80 }
            };
        quayTiles = new int[][]{{ 61, 62, 62, 62, 63 },
                                { 66, 67, 68, 68, 65 },
                                { 0, 64, 69, 69, 70 },
                                { 0, 71, 72, 72, 73 }
                               };
        try {
            barIMG = Image.createImage("/bar.png");
            bieutuongIMG = Image.createImage("/bieutuong.png");
            gheIMG = Image.createImage("/ghe.png");
            banIMG = Image.createImage("/ban.png");
            cayIMG = Image.createImage("/caycanh.png");
            noIMG = Image.createImage("/no.png");
            chiduongIMG = Image.createImage("/chiduong.png");
            bomIMG = Image.createImage("/bom.png");
            maubanIMG = Image.createImage("/mauban.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        map = new TiledLayer(29, 23, barIMG, 16, 16);
        for (int row = 0; row < 23; row++) {
            for (int col = 0; col < 29; col++) {
                map.setCell(col, row, tiles[row][col]);
            }
        }
        quay = new TiledLayer(5, 4, barIMG, 16, 16);
       
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 5; col++)
                quay.setCell(col, row, quayTiles[row][col]);
        }
        buc = new TiledLayer(6, 3, barIMG, 16, 16);
        int[][] tiles1 = {
                        { 47, 18, 18, 18, 23, 0 },
                        { 40, 40, 40, 40, 40, 0 },
                        { 40, 40, 40, 40, 40, 23 }
                        };
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 6; col++){
                 buc.setCell(col, row, tiles1[row][col]);
            }
        }        
        no = new Sprite(noIMG, 24, 53);
        no.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
        no.setFrame(0);
        chiduong = new Sprite(chiduongIMG, 16, 16);
        chiduong.setFrameSequence(new int[]{12, 13, 14, 15});
        chiduong.setFrame(0);
        bom = new Sprite(bomIMG, 20, 20);
        bom.setFrameSequence(new int[]{0, 1});
        mauban = new Sprite(maubanIMG, 16, 16);
        mauban.setFrameSequence(new int[]{0, 1, 2});
        mauban.setFrame(0);
        gameCanvas.getMrL().setPosition(2 * 16, 10 * 16);
        quay.setPosition(6 * 16, 8 * 16);
        buc.setPosition(0, 15 * 16);
        map.setPosition(0, 0);
        gameCanvas.getLayerManager().append(buc);
        gameCanvas.getLayerManager().append(gameCanvas.getMrL());
        //Khoi tao npc
        /*
        for(int index = 2; index < 6; index ++){
            npc = NPC.elementAt(index);
            //reset lai stage default for npc
            if(npc.type == 2){
                if(index == 2){
                    npc.setPosition(25 * 16, 21 * 16);
                    npc.startC = 25;
                    npc.startR = 21;
                }
                if(index == 3){
                    npc.setPosition(22 * 16, 5 * 16);
                    npc.startC = 22;
                    npc.startR = 5;
                }
            }
            if(npc.type == 3){
                npc.setPosition(13 * 16, 20 * 16);
                npc.startC = 13;
                npc.startR = 20;
                npc.setDefaultStage(3);
            }
            if(npc.type == 4){
                npc.setPosition(20 * 16, 17 * 16);
                npc.startC = 20;
                npc.startR = 17;
                npc.setDefaultStage(4);
            }
            gameCanvas.getLayerManager().append(npc);
        }
         * 
         */
        for(int index = 0; index < 6; index ++){
            if(index == 0){
                if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/cop.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                npc = new NPC(npcIMG, 16, 32, 2);
            }
            if(index == 1){
                npc = new NPC(npcIMG, 16, 32, 2);
                npcIMG = null;
            }
            if (index == 2) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc3.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 2);
                //}
                
                npc.setPosition(25 * 16, 21 * 16);
                npc.startC = 25;
                npc.startR = 21;
            }
            if (index == 3) {        
                npc = new NPC(npcIMG, 16, 32, 2);             
                npc.setPosition(22 * 16, 5 * 16);
                npc.startC = 22;
                npc.startR = 5;
                npcIMG = null;
            }
            if (index == 4) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc1.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 3);
               // }
               
                npc.setPosition(13 * 16, 20 * 16);
                npc.startC = 13;
                npc.startR = 20;
                npcIMG = null;
            }
            if (index == 5) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc2.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 4);
                //}
               
                npc.setPosition(20 * 16, 17 * 16);
                npc.startC = 20;
                npc.startR = 17;
                npcIMG = null;
            }
            if(index != 0 && index != 1)
            gameCanvas.getLayerManager().append(npc);
        }     
        for(int index = 0; index < 17; index ++){
            if(index == 0){
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/gainhay.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 5);
                npc.setPosition(13 * 16, 10 * 16);
                npc.startC = 13;
                npc.startR = 10;
            }
            if(index == 1){
                npc = new NPC(npcIMG, 16, 32, 5);
                npc.setPosition(15 * 16, 10 * 16);
                npc.startC = 15;
                npc.startR = 10;
            }
            if(index == 2){
                npc = new NPC(npcIMG, 16, 32, 5);
                npc.setPosition(14 * 16, 12 * 16);
                npc.startC = 14;
                npc.startR = 12;
                npcIMG = null;
            }
            if(index == 3){
                //gang 1
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/gang1.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 6);
                npc.setFrameSequence(npc.stopSeq);
                npc.setPosition(12 * 16, 5 * 16);
                npc.startC = 12;
                npc.startR = 5;
            }
            if(index == 4){
                npc = new NPC(npcIMG, 16, 32, 6);
                npc.setFrameSequence(npc.stopSeq1);
                npc.setPosition(8 * 16, 13 * 16);
                npc.startC = 8;
                npc.startR = 13;
                npcIMG = null;
            }
            if(index == 5){ 
                //bao ve
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/baove.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 7);
                npc.setPosition(16, 10 * 16);
                npc.startC = 1;
                npc.startR = 10;              
            }
            if(index == 6){
                npc = new NPC(npcIMG, 16, 32, 7);
                npc.setPosition(4 * 16, 10 * 16);
                npc.startC = 4;
                npc.startR = 10;
            }
            if(index == 7){
                npc = new NPC(npcIMG, 16, 32, 7);
                npc.setPosition(16, 13 * 16);
                npc.startC = 1;
                npc.startR = 13;
            }
            if(index == 8){
                npc = new NPC(npcIMG, 16, 32, 7);
                npc.setPosition(8 * 16, 15 * 16);
                npc.startC = 8;
                npc.startR = 15;               
            }
            if(index == 9){
                npc = new NPC(npcIMG, 16, 32, 7);
                npc.setPosition(21 * 16, 11 * 16);
                npc.startC = 21;
                npc.startR = 11; 
                npcIMG = null;
            }
            if(index == 10){
                // casi
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/casi.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 8);
                npc.setPosition(4 * 16, 17 * 16);
                npc.startC = 4;
                npc.startR = 17;
                npcIMG = null;
            }
            if(index == 11){
                //dj
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/dj.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 9);
                npc.setPosition(0, 18 * 16);
                npc.startC = 0;
                npc.startR = 18;
                npcIMG = null;
            }
            if(index == 12){
                //guitar
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/guitar.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 11);
                npc.setPosition(2 * 16, 16 * 16);
                npc.startC = 3;
                npc.startR = 16;
                npcIMG = null;
            }
            if(index == 13){
                // contin
                if(npcIMG == null)
                    try {
                    npcIMG = Image.createImage("/contin.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 10);
                npc.setFrameSequence(npc.stopSeq);
                npc.setPosition(8 * 16, 9 * 16 + 8);
                npc.startC = 8;
                npc.startR = 10;
                //System.out.println("sixe: " + NPC.listNPC.size());
            }
            if(index == 14){
                npc = new NPC(npcIMG, 16, 32, 10);
                npc.setFrameSequence(npc.stopSeq1);
                npc.setPosition(7 * 16, 14 * 16);
                npc.startC = 7;
                npc.startR = 14;
                npcIMG = null;
            }
            if(index == 15){
                //dan em
                if(npcIMG == null)
                    try {
                        npcIMG = Image.createImage("/danem.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 12);
                npc.setPosition(20 * 16, 6 * 16);
                npc.startC = 20;
                npc.startR = 6;
                npcIMG = null;
            }
            if(index == 16){
                if(npcIMG == null)
                    try {
                        npcIMG = Image.createImage("/boss.png");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                npc = new NPC(npcIMG, 16, 32, 13);
                npc.setPosition(28 * 16, 8 * 16);
                npc.startC = 28;
                npc.startR = 8;
                npcIMG = null;
            }
            if(index != 12 && index != 10 && index != 13 && index != 15 && index != 16)
                gameCanvas.getLayerManager().append(npc);
            else if(index == 12 || index == 10){
                gameCanvas.getLayerManager().insert(npc, 0);
            }
            else if(index == 15 || index == 16){
                gameCanvas.getLayerManager().insert(npc, 0);
            }
        }
        //-------------------
        for(int index = 0; index < listbanghecay.capacity(); index ++){
            if(index < 16){
                banghecay = new Sprite(gheIMG, 16, 20);
                banghecay.setPosition(mapXY[index][1] * 16, mapXY[index][0] * 16);
                gameCanvas.getLayerManager().append(banghecay);
                listbanghecay.addElement(banghecay);
            }
            else if(index < 20){
                banghecay = new Sprite(banIMG, 16, 48);
                banghecay.setPosition(mapXY[index][1] * 16, mapXY[index][0] * 16);
                gameCanvas.getLayerManager().append(banghecay);
                listbanghecay.addElement(banghecay);
            }
            else{
                //{6, 5}, {6, 11}, {11, 6}, {22, 5}, {18, 18}, {13, 17}, {22, 28}
                banghecay = new Sprite(cayIMG, 16, 41);
                if(index == 20 || index == 26 )
                    banghecay.setPosition(mapXY[index][1] * 16 - 4 , (mapXY[index][0] + 1 )* 16 - banghecay.getHeight() );
                else if(index == 25)
                    banghecay.setPosition(mapXY[index][1] * 16  , (mapXY[index][0] + 1 )* 16 - banghecay.getHeight() - 8 );
                else
                    banghecay.setPosition(mapXY[index][1] * 16  , (mapXY[index][0] + 1 )* 16 - banghecay.getHeight() );
                gameCanvas.getLayerManager().append(banghecay);
                listbanghecay.addElement(banghecay);
            }
        }
        System.out.println("size: " + NPC.listNPC.size() + "/cay: " + listbanghecay.size());
        gameCanvas.getLayerManager().append(quay);
        npc = NPC.elementAt(19);
        //System.out.println("type: " + npc.type);
        gameCanvas.getLayerManager().append(npc);
        hieuung = new Sprite(gameCanvas.getHieungIMG(), 35, 35);
        hieuung.setFrameSequence(new int[]{0, 1, 2});
        hieuung.setFrame(0);
        hieuung.setPosition(16 * 16 - 8, 13 * 16 + 8);
        gameCanvas.getLayerManager().append(hieuung);
        gameCanvas.getLayerManager().append(map);
        //demo
        gameCanvas.updateToTuido(13); //bom
    }
    public void npcAction(SunnetGameCanvas gameCanvas){
        for(int index = 2; index < NPC.listNPC.size(); index ++){
            npc = NPC.elementAt(index);
            if(!npc.isdie){
                if(npc.type != 3 && npc.type != 4 && npc.type != 12 && npc.type!= 13)
                    npc.nextFrame();
                if (npc.type == 3) {
                    if(!npc.setaction){
                        if (npc.isMove) {
                            if (npc.key6) {
                                npc.move(2, 0);
                            }
                            if (npc.key4) {
                                npc.move(-2, 0);
                            }
                            npc.nextFrame();
                        }
                        if (npc.getX() == (npc.startC + 4) * 16) {
                            if (npc.isMove) {
                                npc.stopMove();
                                npc.setFrameSequence(npc.stopSeq);
                            }
                            npc.nextFrame();
                            npc.timeStop++;
                            if (npc.getFrame() == 0 && npc.timeStop >= 100) {
                                npc.isMove = true;
                                npc.setFrSeqForKey6 = false;
                                npc.timeStop = 0;
                                if (!npc.setFrSeqForKey4) {
                                    npc.setFrSeqForKey4 = true;
                                    npc.setFrameSequence(npc.seqFor4);
                                    npc.key4 = true;
                                    npc.key6 = false;
                                }
                            }
                        }
                        if (npc.getX() == (npc.startC - 2) * 16) {
                            if (npc.isMove) {
                                npc.stopMove();
                                npc.setFrameSequence(npc.stopSeq);
                            }
                            npc.nextFrame();
                            npc.timeStop++;
                            if (npc.getFrame() == 0 && npc.timeStop >= 100) {
                                npc.timeStop = 0;
                                npc.isMove = true;
                                npc.setFrSeqForKey4 = false;
                                if (!npc.setFrSeqForKey6) {
                                    npc.setFrSeqForKey6 = true;
                                    npc.setFrameSequence(npc.seqFor6);
                                    npc.key4 = false;
                                    npc.key6 = true;
                                }
                            }
                        }
                    }
                    else{
                        npc.nextFrame();
                    }
                }
                if(npc.type == 4){
                    if(!npc.setaction){
                        if (npc.isMove) {
                            if (npc.key2) {
                                npc.move(0, -2);
                            }
                            if (npc.key8) {
                                npc.move(0, 2);
                            }
                            npc.nextFrame();
                        }
                        if (npc.getY() == (npc.startR - 4) * 16) {
                            npc.setFrSeqForKey2 = false;
                            npc.timeStop = 0;
                            if (!npc.setFrSeqForKey8) {
                                npc.setFrameSequence(npc.seqFor8);
                                npc.setFrSeqForKey8 = true;
                                npc.key8 = true;
                                npc.key2 = false;
                            }
                        }
                        if (npc.getY() == (npc.startR + 1) * 16) {
                            if (npc.isMove) {
                                npc.stopMove();
                                npc.setFrameSequence(npc.stopSeq);
                            }
                            if (npc.getFrame() != 7 && !npc.nextFrame) {
                                npc.nextFrame();
                            }
                            if(npc.getFrame() == 7){
                                npc.timeStop++;
                                npc.nextFrame = true;
                            }
                            if (npc.timeStop >= 100) {
                                npc.prevFrame();
                                if(npc.getFrame() == 0){
                                    npc.nextFrame = false;
                                    npc.timeStop = 0;
                                    npc.isMove = true;
                                    npc.setFrSeqForKey8 = false;
                                    if (!npc.setFrSeqForKey2) {
                                        npc.setFrameSequence(npc.seqFor2);
                                        npc.setFrSeqForKey2 = true;
                                        npc.key8 = false;
                                        npc.key2 = true;
                                    }
                                }
                            }
                        }
                    }
                    else{
                        npc.nextFrame();
                    }
                }
            }
            if(bossmove){
                chiduong.setPosition(23 * 16 + 8 , 19 * 16);
                gameCanvas.getLayerManager().insert(chiduong, gameCanvas.getLayerManager().getSize() - 2);
                chiduong.nextFrame();
                if(npc.type == 13){
                    //boss
                    npc.nextFrame();
                    if(npc.key2){
                        npc.move(0, -2);
                    }
                    if(npc.key4){
                        npc.move(-2, 0);
                    }
                    if(npc.key6){
                        npc.move(2, 0);
                    }
                    if(npc.key8){
                        npc.move(0, 2);
                    }
                    if(!npc.isMove){
                        if(npc.getFrame() == 3){
                            npc.isMove = true;
                            npc.setFrameSequence(npc.seqFor4);
                            npc.key4 = true;
                        }
                    }
                    if(npc.getX() == (npc.startC - 8) * 16){
                        if(!npc.setFrSeqForKey8){
                            npc.setFrameSequence(npc.seqFor8);
                            npc.setFrSeqForKey8 = true;
                            npc.key8 = true;
                            npc.key4 = false;
                            demove = true;
                        }
                    }
                    if(npc.getY() == (npc.startR + 12) * 16){
                        if(!npc.setFrSeqForKey6){
                            npc.setFrSeqForKey6 = true;
                            npc.setFrameSequence(npc.seqFor6);
                            npc.key6 = true;
                            npc.key8 = false;
                        }
                        if(npc.getX() == (npc.startC - 4) * 16){
                            if(!npc.setFrSeqForKey2){
                                npc.setFrSeqForKey2 = true;
                                npc.setFrameSequence(npc.seqFor2);
                                npc.key2 = true;
                                npc.key6 = false;
                            }
                        }
                    }
                    if(npc.getY() == (npc.startR + 9) * 16 && npc.getX() == (npc.startC - 4) * 16){
                        gameCanvas.getLayerManager().remove(npc);
                        bossmove = false;
                        npc.key2 = false;
                    }
                }
                
            }
            if(demove){
                if(npc.type == 12){
                    //dan em
                    npc.nextFrame();
                    if(npc.key2){
                        npc.move(0, -2);
                    }
                    if(npc.key4){
                        npc.move(-2, 0);
                    }
                    if(npc.key6){
                        npc.move(2, 0);
                    }
                    if(npc.key8){
                        npc.move(0, 2);
                    }
                    if(!npc.setFrSeqForKey8){
                        npc.setFrameSequence(npc.seqFor8);
                        npc.setFrSeqForKey8 = true;
                        npc.key8 = true;
                    }
                    if(npc.getY() == (npc.startR + 14)* 16){
                        if(!npc.setFrSeqForKey6){
                            npc.setFrameSequence(npc.seqFor6);
                            npc.setFrSeqForKey6 = true;
                            npc.key8 = false;
                            npc.key6 = true;
                        }
                        if(npc.getX() == (npc.startC + 3) * 16){
                            if(!npc.setFrSeqForKey2){
                                npc.setFrameSequence(npc.seqFor2);
                                npc.setFrSeqForKey2 = true;
                                npc.key6 = false;
                                npc.key2 = true;
                            }                           
                        }
                    }
                    if(npc.getY() == (npc.startR + 11) * 16 && npc.getX() == (npc.startC + 3) * 16){
                        npc.key2 = false;
                        gameCanvas.getLayerManager().remove(npc);
                        gameCanvas.getLayerManager().remove(chiduong);
                        demove = false;
                        gameCanvas.isnmap = true;
                    }
                }
            }
            if(npc.isdie){
                if(npc.getFrame() != npc.dieSeq.length - 1){
                    npc.nextFrame();
                }
            }
            //Xet sau mot su kien moi xay ra
            if(iswar){
                npc.waitForDie ++;
                if(npc.type == 3){                   
                    if(!npc.setaction){
                        npc.setFrameSequence(npc.action1);
                        npc.setaction = true;
                    }
                    if(!npc.setdieSeq){
                        if(npc.waitForDie >= 80){
                            npc.setFrameSequence(npc.dieSeq1);
                            npc.setdieSeq = true;
                            npc.isdie = true;
                            npc.dieSeq = npc.dieSeq1;
                        }
                    }
                }
                if(npc.type == 4){
                    if(!npc.setaction){
                        npc.setFrameSequence(npc.action1);
                        npc.setaction = true;
                    }
                    if(!npc.setdieSeq){
                        if(npc.waitForDie >= 100){
                            npc.setFrameSequence(npc.dieSeq1);
                            npc.setdieSeq = true;
                            npc.isdie = true;
                            npc.dieSeq = npc.dieSeq1;
                        }
                    }
                }
                if(npc.type == 5){
                    //gai nhay
                    if(npc.startC == 13 || npc.startC == 15){
                        if(npc.waitForDie >= 20){
                            if(!npc.setdieSeq){
                                npc.setFrameSequence(npc.dieSeq1);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                                npc.waitForDie = 0;
                            }
                        }
                    }
                    if(npc.startC == 14){
                        if(npc.waitForDie >= 20){
                            if(!npc.setdieSeq){
                                npc.setFrameSequence(npc.dieSeq2);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                            }
                            npc.waitForDie = 0;
                        }
                    }
                }
                if(npc.type == 6){
                    //gang1
                    if(npc.startC == 12){
                        if(!npc.setaction){
                            npc.setFrameSequence(npc.action1);
                            npc.setaction = true;
                        }
                        if(!npc.setdieSeq){
                            if(npc.waitForDie >= 50){
                                npc.setFrameSequence(npc.dieSeq1);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                            }
                        }
                    }
                    if(npc.startC == 8){
                        if(!npc.setaction){
                            npc.setFrameSequence(npc.action2);
                            npc.setaction = true;
                        }
                        if(!npc.setdieSeq){
                            if(npc.waitForDie >= 30){
                                npc.setFrameSequence(npc.dieSeq1);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                            }
                        }
                    }
                }
                if(npc.type == 7){
                    //bao ve
                    if(npc.startC == 1 || npc.startC == 4){
                        if(!npc.setaction){
                            npc.setFrameSequence(npc.action2);
                            npc.setaction = true;
                        }
                        if(!npc.setdieSeq){
                            if(npc.waitForDie >= 40){
                                npc.setFrameSequence(npc.dieSeq2);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq2;
                            }
                        }
                       
                    }
                    if(npc.startC == 8 || npc.startC == 21){
                        if(!npc.setaction){
                            npc.setFrameSequence(npc.action1);
                            npc.setaction = true;
                        }
                        if(!npc.setdieSeq){
                            if(npc.waitForDie >= 50){
                                npc.setFrameSequence(npc.dieSeq1);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                            }
                        }
                    }

                }
                if(npc.type == 8){
                    //casi
                    if(npc.waitForDie >= 20){
                        if(!npc.setdieSeq){
                            npc.setFrameSequence(npc.dieSeq1);
                            npc.setdieSeq = true;
                            npc.isdie = true;
                            npc.dieSeq = npc.dieSeq1;
                            npc.waitForDie = 0;
                        }
                    }
                }
                if(npc.type == 9){
                    //dj
                    if(!npc.setaction){
                        npc.setFrameSequence(npc.action1);
                        npc.setaction = true;
                    }
                    if(!npc.setdieSeq){
                        if(npc.waitForDie >= 100){
                            npc.setFrameSequence(npc.dieSeq1);
                            npc.setdieSeq = true;    
                            npc.isdie = true;
                            npc.dieSeq = npc.dieSeq1;
                            iswar = false; // ket thuc giao tranh
                            viewPortY = savePortY;
                            viewPortX = savePortX;                          
                            talkBoss = true;
                        }
                    }
                }
                if(npc.type == 10){
                    //contin
                    if(npc.startC == 8){
                        if(npc.waitForDie >= 20){
                            if(!npc.setdieSeq){
                                npc.setFrameSequence(npc.dieSeq1);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                                npc.waitForDie = 0;
                            }
                        }
                    }
                    if(npc.startC == 7){
                        if(npc.waitForDie >= 20){
                            if(!npc.setdieSeq){
                                npc.setFrameSequence(npc.dieSeq2);
                                npc.setdieSeq = true;
                                npc.isdie = true;
                                npc.dieSeq = npc.dieSeq1;
                                npc.waitForDie = 0;
                            }
                        }
                    }
                }
                if(npc.type == 11){
                    //guitar
                    if(npc.waitForDie >= 20){
                        if(!npc.setdieSeq){
                            npc.setFrameSequence(npc.dieSeq1);
                            npc.setdieSeq = true;
                            npc.isdie = true;
                            npc.dieSeq = npc.dieSeq1;
                            npc.waitForDie = 0;
                        }
                    }
                }
            }
        }
    }
    public void getColides(SunnetGameCanvas gameCanvas){
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();

        if(mrLX == 0){
            gameCanvas.press4 = false;
            gameCanvas.key4 = false;
        }
        if(mrLX == 28 * 16){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if(mrLY == 21 * 16){
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        if(mrLY == 5 * 16){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        //game Over
        if(mrLX >= 13 * 16 && mrLX <=  17 * 16 && mrLY >= 14 * 16 && mrLY <= 19 * 16){
            if(bomno){
                if(!gameCanvas.gameOver){
                    gameCanvas.getMrL().setFrameSequence(new int[]{34, 35, 36});
                    gameCanvas.gameOver = true;
                }
            }
        }
        if((mrLY > 10 * 16 && mrLX <= 17 * 16) || (mrLX > 17 * 16 && mrLY > 14 * 16)){
            if(iswar){
                blood = true;
                mauban.setPosition(mrLX, mrLY + 8);
                gameCanvas.getLayerManager().insert(mauban, 0);
                mauban.nextFrame();
            }
            else{
                blood = false;
                gameCanvas.getLayerManager().remove(mauban);
            }
        }
        else{
            if(blood){
                blood = false;
                gameCanvas.getLayerManager().remove(mauban);
            }
        }
        //Colides with gid
        if(mrLX <  10 * 16  && mrLY == 10 * 16 - 8){
            gameCanvas.press2 = false;
            gameCanvas.key2 = false;
        }
        //Colides with buc
        if((mrLX == 5 * 16 && mrLY > 14 * 16 && mrLY < 17 * 16)||(mrLX == 6 * 16 && mrLY > 16 * 16 && mrLY < 20 * 16 - 4) || (mrLX == 5 * 16 && mrLY >= 20 * 16 - 4)){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if(mrLY == 20 * 16 - 4 && mrLX < 6 * 16){
            gameCanvas.press2 = false;
            gameCanvas.key2 = false;
        }
        if((mrLY == 15 * 16  && mrLX < 5 * 16) || (mrLY == 16 * 16 && mrLX >= 5 * 16 && mrLX < 6 * 16)){
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        //Colides with quay
        if((mrLX == 6 * 16 && mrLY < 10 * 16 + 8 )||(mrLX == 10 * 16 && mrLY >= 10 * 16  - 8 && mrLY < 11 * 16 + 8) || (mrLX == 13 * 16 && mrLY > 11 * 16 && mrLY <  13 * 16 + 8)){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if((mrLY == 10 * 16 + 8 && mrLX >  6 * 16  && mrLX <= 10 * 16) || (mrLY == 11 * 16 + 8 && mrLX > 10 * 16 && mrLX < 13 * 16) ||(mrLY == 13 * 16 + 8 && mrLX > 13 * 16 && mrLX < 17 * 16 )){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        //Colides with cau thang
        if((mrLX == 16 * 16 && mrLY <  16 * 16 + 8 && mrLY > 12 * 16) || ((mrLX == 18 * 16 || mrLX == 21 * 16) && mrLY < 18 * 16 + 8 && mrLY > 12 * 16 - 4)){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if(mrLY == 16 * 16 + 8 && ((mrLX > 16 * 16 && mrLX <= 18 * 16) || mrLX >= 22 * 16 )){           
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        if((mrLX == 22 * 16 || mrLX == 19 * 16)&& mrLY <  18 * 16 + 8 && mrLY > 12 * 16 - 4){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        //Colides with san
        if((mrLY == 12 * 16 - 4 && ((mrLX > 17 * 16 && mrLX < 19 * 16) || mrLX > 21 * 16)) || (mrLY == 6 * 16 - 8 && mrLX >=11 * 16 && mrLX < 17 * 16)){
            gameCanvas.press8 = false;
            gameCanvas.key8 = false;
        }
        if((mrLX == 17 * 16 && mrLY <  12 * 16 && mrLY > 6 * 16 - 8) || (mrLX == 11 * 16 && mrLY < 6 * 16)){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        //Colides with  ban ghe
        for(int index = 0; index < listbanghecay.size(); index ++){
            banghecay = (Sprite)listbanghecay.elementAt(index);
            if(index < 20){
                // mrLY > banghecay.getY() - 32 + 8  && mrLY < banghecay.getY() + banghecay.getHeight()  - 32 + 4
                if(mrLX == banghecay.getX() - 16 && mrLY > banghecay.getY() - 24  && mrLY < banghecay.getY() + banghecay.getHeight()  -28){
                    gameCanvas.press6 = false;
                    gameCanvas.key6 = false;
                }
                if(mrLX == banghecay.getX() + 16 && mrLY > banghecay.getY() - 24 && mrLY < banghecay.getY()  + banghecay.getHeight()  - 28 ){
                    gameCanvas.press4 = false;
                    gameCanvas.key4 = false;
                }
                if(mrLY == banghecay.getY() - 24  && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.press8 = false;
                    gameCanvas.key8 = false;
                }
                if(mrLY == banghecay.getY() + banghecay.getHeight() - 28 && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.press2 = false;
                    gameCanvas.key2 = false;
                }
                //uplayer for key8
                if(mrLY <= banghecay.getY() - 24 && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.getLayerManager().insert(banghecay, 5);
                }
                if(mrLY > banghecay.getY() - 24 && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.getLayerManager().insert(banghecay, index + 20);
                    gameCanvas.getLayerManager().insert(gameCanvas.getMrL(), 5); // chu y khi remove boss va dan em
                }
            }            
            else{
                if(mrLX == banghecay.getX() - 16 && mrLY > (mapXY[index][0] ) * 16 - 24 && mrLY < (mapXY[index][0] + 1) * 16 - 28){
                    gameCanvas.press6 = false;
                    gameCanvas.key6 = false;
                }
                if(mrLX == banghecay.getX() + 16 && mrLY > (mapXY[index][0] ) * 16 - 24 && mrLY < (mapXY[index][0] + 1) * 16 - 28){
                    gameCanvas.press4 = false;
                    gameCanvas.key4 = false;
                }
                if(mrLY == (mapXY[index][0] ) * 16 - 24  && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.press8 = false;
                    gameCanvas.key8 = false;
                }
                if(mrLY ==(mapXY[index][0] + 1) * 16 - 28 && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.press2 = false;
                    gameCanvas.key2 = false;
                }
                //uplayer for key8                
                if(mrLY <= (mapXY[index][0] ) * 16 - 24  && mrLX > banghecay.getX() - 16  && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.getLayerManager().insert(banghecay, 5); 
                }
                if(mrLY > (mapXY[index][0] ) * 16 - 24 && mrLX > banghecay.getX() - 16 && mrLX < banghecay.getX() + banghecay.getWidth()){
                    gameCanvas.getLayerManager().insert(banghecay, index + 20); 
                    gameCanvas.getLayerManager().insert(gameCanvas.getMrL(), 5);  // chu y khi remove boss va dan em
                }              
            }            
        }
        if(mrLX > 15 * 16 && mrLX < 17 * 16  && mrLY == 13 * 16 + 8){
            useBom = true;
        }
    }
    public void vibrateAct(){
        //Tao do rung man hinh
        if(fv == 1){
            map.setPosition(0, 2);
        }
        if(fv == 2){
            map.setPosition(0, -2);
        }
        if(fv == 4){
            map.setPosition(0, 2);
        }
        if(fv == 6){
            map.setPosition(0, -2);
        }
        if(fv == 8){
            map.setPosition(0, 2);
        }
        if(fv == 10){
            map.setPosition(0, -2);
        }
        if(fv == 12){
            fv = 0;
            vibrate=false;
            map.setPosition(0, 0);
        }
        fv ++;        
    }
    public void talkProcess(Graphics g, SunnetGameCanvas gameCanvas){
        g.setColor(55, 77, 91);
        g.fillRect(20, height - 90, 200, 60);
        if(talking){
            if(talkIndex % 2 == 0)
                gameCanvas.getMatNV().setFrame(0);
            else
                gameCanvas.getMatNV().setFrame(1);
        }
        else if(talkBoss){
            gameCanvas.getMatNV().setFrame(1);
            talkIndex = 4;
        }
        gameCanvas.getMatNV().setPosition(20, height - 85);
        gameCanvas.getMatNV().paint(g);
        gameCanvas.getKhungThoai().setPosition(20, height - 90);
        gameCanvas.getKhungThoai().paint(g);
        if(!isDrawing){
            textT = new byte[talk[talkIndex].length()];    
            Designer.toBytesIndex(talk[talkIndex] , textT);
            isDrawing = true;
            endTextID = 0;
            //System.out.println("boss");
        }
        else{
            if(endTextID < talk[talkIndex].length())
                endTextID ++;
            else if(talkBoss){
                talkBoss = false;
                bossmove = true;
                //System.out.println("bossMove: " + bossmove);
            }
        }
        Designer.drawString(g, textT, 0, endTextID, Designer.FONT_TEXT, Designer.MARGIN_LEFT, false, 2, 75 , height - 85, 150, 50);
    }
    public void useItemInTuido(Graphics g, SunnetGameCanvas gamCanvas){
        if(gamCanvas.useItem[13]){
            if(!bomno && no.getFrame() != 7){
                gamCanvas.getLayerManager().remove(hieuung);
                isDrawTime = true;
                bom.setPosition(16 * 16, 14 * 16);              
                gamCanvas.getLayerManager().insert(bom, gamCanvas.getLayerManager().getSize() - 2);
                bom.nextFrame();
                chiduong.setPosition(20 * 16 , 20 * 16);
                gamCanvas.getLayerManager().insert(chiduong, gamCanvas.getLayerManager().getSize() - 2);
                chiduong.nextFrame();
                timeForBomno ++;
                if(timeForBomno % 10 == 0)
                    tgcount ++;
                if(tgcount >= 10){
                    bomno = true;
                    isDrawTime = false;
                    timeForBomno = 0;
                    vibrate = true;
                    no.setPosition(16 * 16, 13 * 16 - 8);
                    gamCanvas.getLayerManager().insert(no, gamCanvas.getLayerManager().getSize() - 2);
                    gamCanvas.getLayerManager().remove(bom);
                }
            }
            if(bomno){
                no.nextFrame();
                if(no.getFrame() == 7){
                    bomno = false;
                    talking = true;
                    gamCanvas.getLayerManager().remove(chiduong);                   
                }
            }
        }
    }
    public void drawMap(SunnetGameCanvas gameCanvas, Graphics g){
        mrLY = gameCanvas.getMrL().getY();
        mrLX = gameCanvas.getMrL().getX();
        if(!iswar && !talking){
            if (mrLY + height / 2 < 25 * 16 && mrLY - height/2 > 0) {
                viewPortY = mrLY- height / 2;
                savePortY = viewPortY;
            }
            if(mrLX - width / 2 > 0 && (29 * 16 - mrLX > width / 2)){
                viewPortX = mrLX - width / 2;
                savePortX = viewPortX;
            }
        }
        gameCanvas.getLayerManager().setViewWindow(viewPortX, viewPortY, width, height);
        gameCanvas.getLayerManager().paint(g, 0, 0);
        g.drawImage(bieutuongIMG, 7 * 16 - viewPortX,  - viewPortY, 0);
        npcAction(gameCanvas);
        if(vibrate){
            vibrateAct();
        }
        hieuung.nextFrame();
        useItemInTuido(g, gameCanvas);
        if(talking && !gameCanvas.gameOver){
            viewPortX = 0;
            viewPortY = 90;
            talkProcess(g, gameCanvas);
        }
        if(talkBoss){
            talkProcess(g, gameCanvas);
        }
    }
    public void freeMemory(){

        map = null; quay = null; buc = null; hieuung = null; tiles = null; quayTiles = null; listbanghecay = null;
        npc = null; no = null; bom = null; chiduong = null; mauban = null; talk = null; mapXY = null;

        barIMG = null; bieutuongIMG = null; banIMG = null; gheIMG = null; banghecay = null; cayIMG = null;
        npcIMG = null; chiduongIMG = null; noIMG = null; bomIMG = null; maubanIMG = null;
        Runtime.getRuntime().freeMemory();     
    }
}
